package com.hjb.service.controller.siteuseraccount;

import com.hjb.service.config.properties.BaofooProperties;
import com.hjb.service.config.properties.HYLProperties;
import com.hjb.service.domain.area.FYCity;
import com.hjb.service.domain.area.FYCityExample;
import com.hjb.service.domain.area.FYProvince;
import com.hjb.service.domain.siteuser.SiteUser;
import com.hjb.service.domain.siteuser.SiteUserExample;
import com.hjb.service.domain.siteuseraccount.SiteUserBankcard;
import com.hjb.service.domain.siteuseraccount.SiteUserBankcardExample;
import com.hjb.service.gateway.baofoo.BaoFooPayService;
import com.hjb.service.gateway.hyl.HylService;
import com.hjb.service.gateway.hyl.base.HYLResponseVO;
import com.hjb.service.mapper.FYCityMapper;
import com.hjb.service.mapper.FYProvinceMapper;
import com.hjb.service.mapper.SiteUserBankcardMapper;
import com.hjb.service.mapper.SiteUserMapper;
import com.hjb.service.siteuseraccount.SiteUserBankcardService;
import com.hjb.service.utils.DateUtil;
import com.hjb.service.utils.MD5Utils;
import com.hjb.service.utils.MapUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户绑卡 Controller
 * @author gy
 * @date 2017-07-25
 * @version 1.0
 */
@RestController
@RequestMapping("/situserbankcard")
public class SiteUserBankcardController {

	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private SiteUserBankcardService siteUserBankcardService;
	@Autowired
	private HylService hylService;
	@Autowired
	private HYLProperties hylProperties;
	@Autowired
	private FYProvinceMapper fyProvinceMapper;
	@Autowired
	private FYCityMapper fyCityMapper;
	@Autowired
	private SiteUserMapper siteUserMapper;
	@Autowired
	private SiteUserBankcardMapper siteUserBankcardMapper;

	/**
	 * 获取省信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/getProvince")
	@ResponseBody
	public Map<String, Object> getProvince(HttpServletRequest request){
		Map<String,Object> resMap = MapUtil.getRetMap();
		try {
			List<FYProvince> list = fyProvinceMapper.selectAll();
			if (list != null && list.size()>0){
				resMap.put("data",list);
			}else {
				resMap.put("result_code", MapUtil.INTF_ERR_CODE);
				resMap.put("result_info", "无相关数据");
			}
		} catch (Exception e) {
			log.info(e.getMessage());
			resMap.put("result_code", MapUtil.INTF_ERR_CODE);
			resMap.put("result_info", "调用失败");
		}
		return resMap;
	}

	/**
	 * 获取市信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCity")
	@ResponseBody
	public Map<String, Object> getCity(HttpServletRequest request){
		Map<String,Object> resMap = MapUtil.getRetMap();
		try {
			String provinceCode = request.getParameter("provinceCode");
			FYCityExample fyCityExample = new FYCityExample();
			fyCityExample.createCriteria().andProvinceCodeEqualTo(provinceCode);
			List<FYCity> list = fyCityMapper.selectByExample(fyCityExample);
			if (list != null && list.size()>0){
				resMap.put("data",list);
			}else {
				resMap.put("result_code", MapUtil.INTF_ERR_CODE);
				resMap.put("result_info", "无相关数据");
			}
		} catch (Exception e) {
			log.info(e.getMessage());
			resMap.put("result_code", MapUtil.INTF_ERR_CODE);
			resMap.put("result_info", "调用失败");
		}
		return resMap;
	}

	/**
	 * 用户绑卡
	 * @param request
	 * @return
	 */
	@RequestMapping("/bind")
	@ResponseBody
	public Map<String, Object> bindBankCard(HttpServletRequest request){
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("result_code",MapUtil.INTF_ERR_CODE);
		resMap.put("result_info", "绑卡失败");
		try {
			SiteUser siteUser = (SiteUser) request.getSession().getAttribute("siteUser");
			if (siteUser == null){
				resMap.put("result_code",MapUtil.INTF_ERR_CODE_416);
				resMap.put("result_info", "绑卡失败，请重新登录");
				return resMap;
			}
			SiteUserBankcardExample siteUserBankcardExample = new SiteUserBankcardExample();
			siteUserBankcardExample.createCriteria().andStatusEqualTo(1).andUnbindStatusEqualTo(0).andCardNoEqualTo(request.getParameter("cardNo")).andRemainPhoneEqualTo(request.getParameter("mobile"));
			List<SiteUserBankcard> siteUserBankcards = siteUserBankcardMapper.selectByExample(siteUserBankcardExample);
			if (siteUserBankcards!=null && siteUserBankcards.size()>0){
				resMap.put("result_code",MapUtil.INTF_ERR_CODE);
				resMap.put("result_info", "已绑卡，请重新登录");
				return resMap;
			}
			Map<String,String> reqParams = getHYLBindReqParam(request);
			String merchNo = hylProperties.getMerchNo();//商户号
			String orderno = merchNo+ DateUtil.now(DateUtil.FORMAT_LONG_YMD)+Math.round((9999999*Math.random()));
			reqParams.put("mhtOrderNo", orderno);
			reqParams.put("useId", siteUser.getUserId().toString());
			//reqParams.put("useId", "2001");
			//请求好易联三方鉴权接口
			HYLResponseVO hylResponseVO = hylService.submitVerify(reqParams);
			//hylService.verifyQuery(orderno);
			//业务处理
			if ("0000".equals(hylResponseVO.getResponseCode()) && "00".equals(hylResponseVO.getStatus())){
				//维护用户信息
				resMap = siteUserBankcardService.bindBankCard(reqParams,"");
				/*Map<String,String> reqParam = getBaoFooBindReqParam(request);
				//宝付鉴权
				Map<String, Object> arrayDataString = baoFooPayService.bindCard(reqParam);
				if("0000".equals(arrayDataString.get("resp_code"))){
					//维护用户信息
//					resMap = siteUserBankcardService.bindBankCard(reqParams,arrayDataString.get("bind_id").toString());
				}else {
					resMap.put("result_code",MapUtil.INTF_ERR_CODE);
					resMap.put("result_info", arrayDataString.get("resp_msg"));
				}*/
				SiteUserExample siteUserExample = new SiteUserExample();
				siteUserExample.createCriteria().andMobileEqualTo(siteUser.getMobile()).andLoginPasswordEqualTo(siteUser.getLoginPassword());
				List<SiteUser> siteUserList = siteUserMapper.selectByExample(siteUserExample);
				if (siteUserList != null && siteUserList.size() > 0) {
					request.getSession().setAttribute("siteUser", siteUserList.get(0));
				}
			}else {
				resMap.put("result_code",MapUtil.INTF_ERR_CODE);
				resMap.put("result_info", hylResponseVO.getResponseMsg());
			}
		} catch (Exception e) {
			log.info("用户绑卡失败：",e);
			resMap.put("result_code", MapUtil.INTF_ERR_CODE);
			resMap.put("result_info", "调用失败");
		}
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("null","");
		resMap.put("data",data);
		return resMap;
	}

	/**
	 * 用户卡解绑
	 * @param request
	 * @return
	 */
	@RequestMapping("/unbind")
	@ResponseBody
	public Map<String, Object> unbindBankCard(HttpServletRequest request){
		Map<String,Object> resMap = MapUtil.getRetMap();
		Map<String,Object> params = new HashMap<String, Object>();
		try {
			/*SiteUserBankcard dataParams = getBindDataParam(request);
			resMap = siteUserBankcardService.unbindBankCard(dataParams);*/
		} catch (Exception e) {
			log.info(e.getMessage());
			resMap.put("result_code", MapUtil.INTF_ERR_CODE);
			resMap.put("result_info", "调用失败");
		}
		return resMap;
	}

	private Map<String,String> getHYLBindReqParam(HttpServletRequest request){
		String mobile = request.getParameter("mobile");
		String realName = request.getParameter("realName");
		String idCard = request.getParameter("idCard");
		String msgCode = request.getParameter("msgCode");
		String cardNo = request.getParameter("cardNo");
		String bankName = request.getParameter("bankName");
		String proviceCode = request.getParameter("proviceCode");
		String cityCode = request.getParameter("cityCode");
		String bankAddr = request.getParameter("bankAddr");
		String acctType = request.getParameter("acctType");
		String remark = request.getParameter("remark");
		String bankCode = request.getParameter("bankCode");
		Map<String,String> reqParams = new HashMap<String, String>();
		reqParams.put("idCardName", realName);
		reqParams.put("idCard",idCard);
		reqParams.put("bankCardNum",cardNo);
		reqParams.put("mobile",mobile);
		reqParams.put("remark", remark);
		reqParams.put("msgCode", msgCode);    //短信验证码
		reqParams.put("bankName",bankName);
		reqParams.put("proviceCode",proviceCode);
		reqParams.put("cityCode",cityCode);
		reqParams.put("bankAddr",bankAddr);
		reqParams.put("acctType",acctType);
		reqParams.put("bankCode",bankCode);

		/*reqParams.put("remark", "123");  //保留域1
		reqParams.put("", "");  //保留域2
		reqParams.put("idCardName", "李李");
		reqParams.put("idCard","4302251982091900");
		reqParams.put("bankCardNum","6225880122256500");
		reqParams.put("mobile","13910162618");
		reqParams.put("msgCode", "");    //短信验证码
		reqParams.put("bankName","yinhang");
		reqParams.put("proviceCode","111");
		reqParams.put("cityCode","222");
		reqParams.put("bankAddr","beijing");
		reqParams.put("bankCode","102");
		reqParams.put("acctType","22");*/


		return reqParams;
	}
	private Map<String,String> getBaoFooBindReqParam(HttpServletRequest request){
		String mobile = request.getParameter("mobile");
		String realName = request.getParameter("realName");
		String idCard = request.getParameter("idCard");
		String cardNo = request.getParameter("cardNo");
		Map<String,String> reqParams = new HashMap<String, String>();
		/*reqParams.put("idCardName", realName);
		reqParams.put("idCard",idCard);
		reqParams.put("bankCardNum",cardNo);
		reqParams.put("mobile",mobile);*/

		reqParams.put("idName", "李李");
		reqParams.put("idCard","4302251982091900");
		reqParams.put("cardNo","6225880122256500");
		reqParams.put("mobile","13910162618");
		reqParams.put("orderNo","cs"+System.currentTimeMillis());
		reqParams.put("bankCode","ICBC");
		return reqParams;
	}

}
